#!/usr/bin/env ruby
require 'optparse'
options = {}
optparse = OptionParser.new do |opts|
    opts.banner = 'Usage: check_raid_adapter.rb [options]' 
    options[:datafile] = nil
    opts.on('-d', '--datafile DATAFILE', 'file to parse') do |f|
        options[:datafile] = f
    end
    opts.on('-h', '--help', 'display this help menu and exit') do
        puts opts
        exit 1
    end
end
optparse.parse!
datafile = options[:datafile]
if not datafile.nil? and not File.exist? datafile
    puts optparse.help
    exit 1
end
$stdin = File.new(datafile) if datafile
class Adapter
    def initialize id, offline, failed_disks
        @id, @offline, @failed_disks = id, offline.to_i, failed_disks.to_i
    end
    attr_accessor :id, :offline, :failed_disks
    def to_s
        "id: %s, offline: %s, failed_disks: %s" % [@id, @offline, @failed_disks]
    end
    def ok?
        @offline.zero? and @failed_disks.zero?
    end
end
bads = []
$stdin.read.scan(
        /(Adapter\s*#\d).*?Offline\s*:\s*(\d).*?Failed Disks\s*:\s*(\d)/m) do 
    |id, offline, failed_disks|
    adapter = Adapter.new id, offline, failed_disks 
    bads << adapter unless adapter.ok?
end
if bads
    puts "#{bads.length} adapter has error disk"
    bads.each do |bad|
        puts bad
    end
    exit 2
end
puts 'all ok'
exit 0
